import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.print.attribute.standard.MediaSize.Other;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class Update extends JPanel
{
    ArrayList<String> values = new ArrayList<String>();// The selected values.

    private String selected, prevSelected = "";

    private JTextField displayField = new JTextField(30);
    private String tname;
    JFrame frame = new JFrame();

    private JComboBox c = new JComboBox();

    private JButton b = new JButton("Add item");

    final JTextField field = new JTextField("enter changes here");


    private int count = 0, p = 0;;
    String manuf ="" ;
    String model = "", year= "", description = "",litres, inches= "", engine= "";

    ArrayList<String> temp;
    private Connection con;
    private ConnectDb db;
    private String pnum,newPnum;
    private String[] crxFields = {"ARS1",
            "ARS2",
            "ARS3",
            "ARS4",
            "MOD1",
            "MOD2",
            "MOD3",
            "MOD4",
            "BEH1",
            "BEH2",
            "BEH3",
            "BEH4",
            "DAN1",
            "DAN2",
            "DAN3",
    "DAN4"};

    public Update(Connection c, ConnectDb d, String option)
    {
        db = d;
        boolean status = false;
        con = c;
        if(option.equalsIgnoreCase("P"))
        {
            pnum = JOptionPane.showInputDialog("Enter the part number you would like to update: ");
            newPnum = JOptionPane.showInputDialog("Enter the new part number you would like: ");


            String query ="";
            //check if part number exists

            for(int i = 0; i <4; i++)
            {
                String tableName = "";
                if(i==0)
                    tableName = "RDIMARS";
                else if (i==1)
                    tableName = "RDIMBEH";
                else if (i==2)
                    tableName = "RDIMDAN";
                else
                    tableName = "RDIMMOD";

                updatePnum(pnum,newPnum,tableName);
            }
        }

        else if(option.equalsIgnoreCase("O"))
        {
            updateItem();
        }
    }

    private void updatePnum(String pnum, String newPnum, String tname) 
    {
        //String update = "UPDATE "+tname+" SET p_number = "+newPnum+" WHERE p_number = "+pnum+";";
        String update = "UPDATE "+tname+" SET p_number = '"+newPnum+"' WHERE p_number = '" + pnum + "'";


        System.out.println("neiw 141"+newPnum +" --"+update);

        //String updateAll = "UPDATE radcrx set "
        try
        {          

            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update

            //con.commit();
        }	
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }

        for (int i = 0; i < crxFields.length; i++)
        {
            String updateCrx = "UPDATE radcrx set " + crxFields[i] + "='" + newPnum + "' where " + crxFields[i] + "='" + pnum+"'"; 

            try
            {          
                Statement stmnt2 = con.createStatement();
                int rs2 = stmnt2.executeUpdate(updateCrx); // update

                //JOptionPane.showMessageDialog(frame, "Update Success!");
            }   
            catch (SQLException ex) 
            {
                System.err.println(ex.getMessage());
            }
        }

        //done with update, commit
        try
        {
            con.commit();
        }
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }


    public void updateItem()
    {
        final Query q = new Query(con, db);
        final JPanel displayPanel = new JPanel() ; // the upper panel.
        final JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.
        JPanel updatepanel = new JPanel();

        temp  = q.queryManufac();

        int x = temp.size();
        p++;
        for(int i = 0;i<x;i++)
            c.addItem(temp.get(i));

        //Combo box listener.
        c.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                selected = (String) c.getSelectedItem();
                System.out.println("Selected "+selected);
            }
        });

        //Add Item button listener.
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {                       
                selected = (String) c.getSelectedItem();
                System.out.println(selected);
                if(selected == null)
                {
                    selected = "";
                }

                if(count == 0)
                {
                    temp = q.queryModel(selected.trim());
                    tname = q.getTableName();
                    System.out.println(tname);
                }
                else if(count == 1)
                {
                    System.out.println("checking for year__ "+selected.trim());
                    temp = q.queryYear(selected.trim());
                }
                else if(count == 2)
                {
                    temp = q.queryLitres(selected.trim());
                }
                else if(count == 3)
                {
                    System.out.println("selected:" + selected);
                    temp = q.queryEngineType(selected.trim());
                }
                else if(count == 4)
                    temp = q.queryCubicInches(selected.trim());
                else if(count == 5)
                    temp = q.queryDesc(selected.trim());
                else if(count == 6)
                    temp = q.queryRlink(selected.trim());

                prevSelected = selected;
                
                c.removeAllItems();

                for(int i = 0; i < temp.size(); i++)
                {
                    c.addItem(temp.get(i));
                }
                //		updateBox();

                count++;
            }
        });

        optionPanel.setLayout(new FlowLayout());
        displayPanel.add(displayField);
        optionPanel.add(c);
        optionPanel.add(b);
        //connectDb.disconnectFromDB();


        updatepanel.add(field);
        JButton button = new JButton("update");
        updatepanel.add(button);
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                //execute the update
                String update = field.getText().trim();
                String old = selected;

                System.out.println(count);
                if(count == 1)
                {
                    System.out.println(tname);
                    updateModel(old, update);
                    temp = q.queryModel(prevSelected.trim());

                }
                /*
                else if(count ==2)
                {

                    updateYear(old, update, tname);

                    temp = q.queryYear(prevSelected.trim());

                }*/
                else if(count ==3)
                {

                    updateLitre(old, update, tname);

                    temp = q.queryLitres(prevSelected.trim());

                }
                else if(count ==4)
                {

                    updateEngine (old, update, tname);

                    temp = q.queryEngineType(prevSelected.trim());

                }
                else if(count ==5)
                {

                    updateInches(old, update, tname);

                    temp = q.queryCubicInches(prevSelected.trim());

                }
                else if(count ==6)
                {

                    updateDesc(old, update, tname);

                    temp = q.queryDesc(prevSelected.trim());

                }
                c.removeAllItems();

                for(int i = 0; i < temp.size(); i++)
                {
                    c.addItem(temp.get(i));
                }

            }
        });

        frame.add(updatepanel, BorderLayout.SOUTH);
        frame.add(optionPanel,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }



    public void updateModel(String model,String newmodel)
    {
        System.out.println(tname);
        //String tempTableCrx = "CREATE TABLE temp_crx AS SELECT * FROM"+tname;
        String update = "UPDATE "+tname+" SET MODEL ='"+newmodel+"' WHERE MODEL = '"+model.trim()+ "'";
        System.out.println(update);
        try
        {          

            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update 
        }	
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }	

    public void updateDesc(String desc,String newdesc,String tname)
    {
        String update = "UPDATE "+tname+" SET DESCRIPTION ='"+newdesc+"' WHERE DESCRIPTION = '"+desc+ "'";
        System.out.println(update);

        try
        {          


            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update 
        }	
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }	

    public void updateLitre(String lit,String newlir,String tname)
    {
        String update = "UPDATE "+tname+" SET LITRES ='"+newlir+"' WHERE LITRES = '"+lit + "'";
        System.out.println(update);

        try
        {          

            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update 
        }	
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }	

    public void updateEngine(String e,String newe,String tname)
    {
        String update = "UPDATE "+tname+" SET ENGINE_TYPE ='"+newe+"' WHERE ENGINE_TYPE = '"+e + "'";
        System.out.println(update);

        try
        {          

            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update 
        }	
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }	

    public void updateInches(String in,String newIn,String tname)
    {
        String update = "UPDATE "+tname+" SET CUBIC_INCHES ='"+newIn+"' WHERE CUBIC_INCHES = '"+in +"'";
        System.out.println(update);

        try
        {          

            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update 
        }	
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }

    public void updateYear(String yr,String newYr,String tname)
    {
        String update = "UPDATE "+tname+" SET  YEAR ='"+newYr+"' WHERE YEAR = '"+yr + "'";
        System.out.println(update);

        try
        {          

            Statement stmnt2 = con.createStatement();
            int rs2 = stmnt2.executeUpdate(update); // update 
        }	
        catch (SQLException ex) 
        {
            System.err.println(ex.getMessage());
        }
    }

}
